<?php
/**
 * Copyright (c) 不争网络
 *
 * @Description     个人中心.
 * @Author          Evelyn丶好久不见
 * @Copyright       Copyright (c) 不争网络 保留所有版权()
 * @Date:           2019/4/28
 */
namespace app\api\controller;
use app\common\controller\CommonApi;
use think\Db;
class Car extends CommonApi{
//购物车列表
    public function Carlist(){
    	$where = [];
    	$type = input('status',3,'intval');
    	if($type != 3){
    		$where['c.status'] = ['eq',$type];
    	}

    	$where['user_id']=['eq',UID];

        $list = db('shop_car')->alias('c')->join('goods g','g.id =c.goods_id','left')->join('goods_type t','c.type = t.id','left')->field('c.id as car_id,g.id as goods_id,price,banner as image,title,t.type,num,c.status')->where($where)->select();

        foreach ($list as $key => $value) {
            $list[$key]['image'] = $this->host.explode(',',$value['image'])[0];
        }
        $this->apiReturn(1,'购物车列表',['list'=>$list,'goods_num'=>db('shop_car')->where(['user_id'=>UID])->sum('num')]);
    }




//加入购物车
    public function addCar(){
    	$id = input('id',0,'intval');
    	$num= input('num',1);
    	$info = db('goods')->where(['id'=>$id])->find();
    	$type = input('type',0);

    	if(!$info){
    		$this->apiReturn(0,'商品信息错误');
    	}
    	if(db('goods_type')->where(['goods_id'=>$id,'id'=>$type])->find()==''){
    		$this->apiReturn(0,'商品规格信息错误');
    	}
    	
    	if($info['surplus_num']<$num){
    		$this->apiReturn(0,'库存不足');
    	}

    	if(db('shop_car')->where(['user_id'=>UID,'goods_id'=>$id,'type'=>$type])->find()){
    		$query = db('shop_car')->where(['user_id'=>UID,'goods_id'=>$id,'type'=>$type])->setInc('num',$num);
    	}else{
    		$query = db('shop_car')->insertGetid(['user_id'=>UID,'add_time'=>time(),'goods_id'=>$id,'type'=>$type,'num'=>$num]);
    	}
    	
    	if(!$query){
    		$this->apiReturn(0,'添加失败');
    	}
        $newinfo = db('shop_car')->alias('c')->join('goods g','g.id =c.goods_id','left')->join('goods_type t','c.type = t.id','left')->field('c.id as car_id,g.id as goods_id,price,banner as image,title,t.type,num,c.status')->where(['user_id'=>UID,'c.goods_id'=>$id,'c.type'=>$type])->find();
        $img = explode(',',$newinfo['image']);
        if(empty($img) == false)
            $newinfo['image'] = $this->host.$img[0];
        else{
            $newinfo['image'] = '';
        }
    	$this->apiReturn(1,'操作成功',$newinfo);
    }

//修改购物车数量
    public function editcar(){
    	$id = input('id',0);
    	$info = db('shop_car')->where(['id'=>$id])->find();
    	if(!$info)
    		$this->apiReturn(0,'信息错误');
    	$num = input('num',0);
    	if($num<1){
    		$this->apiReturn(0,'数量不能少于1件');
    	}
    	if(db('goods')->where(['id'=>$info['goods_id']])->value('surplus_num')<$num){
    		$this->apiReturn(0,'库存不足');
    	}

    	$query = db('shop_car')->where(['id'=>$id,'user_id'=>UID])->update(['num'=>$num]);
    	if(!$query){
    		$this->apiReturn(0,'操作失败');
    	}
    	$this->apiReturn(1,'操作成功');
    }

//删除购物车商品
    public function delcar(){
        $id = input('id','');
        if($id == ''){
            $this->apiReturn(0,'信息错误');
        }
        
        $query = db('shop_car')->where('id','in',explode(',',$id))->where(['user_id'=>UID])->delete();
        if(!$query)
            $this->apiReturn(0,'操作失败');
        $this->apiReturn(1,'操作成功');
    }

//删除购物车失效商品
    public function delcars(){
       
        $info = db('shop_car')->where(['user_id'=>UID,'status'=>0])->select();
        if(!$info)
            $this->apiReturn(1,'操作成功');
        $query = db('shop_car')->where(['user_id'=>UID,'status'=>0])->delete();
        if(!$query)
            $this->apiReturn(0,'操作失败');
        $this->apiReturn(1,'操作成功');
    }
}